package com.ai.trickycards.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import com.ai.trickycards.databinding.DialogGameOverBinding
import com.ai.trickycards.entity.enumeration.GameMode
import com.ai.trickycards.utils.Utils

/**
 * @param gameMode 游戏模式
 * @param winnerIndex 胜利玩家的索引
 * @param loserIndex 失败玩家的索引
 * @param rewardScore 人类获得的积分
 * @param beforeLevel 人类之前的等级
 * @param newLevel 人类本局结束后的等级
 */
@SuppressLint("SetTextI18n")
class GameOverDialog(
    context: Context,
    private val gameMode: GameMode,
    private val winnerIndex: Int,
    private val loserIndex: Int,
    private val rewardScore: Int,
    private val beforeLevel: Int,
    private val newLevel: Int,
    private val clickBlock: () -> Unit
) : Dialog(context) {

    private val binding by lazy {
        DialogGameOverBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        window?.let {
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            val displayMetrics = context.resources.displayMetrics
            it.attributes.width = (displayMetrics.widthPixels - 80 * displayMetrics.density).toInt()
        }

        binding.tvOption.setOnClickListener {
            dismiss()
            clickBlock.invoke()
        }

        if (winnerIndex == 0) {
            binding.tvWinnerValue.text = "You"
            binding.tvLoserValue.text = "Player $loserIndex"
            binding.tvCongratulations.text = "Congratulations"
            binding.tvReward.text = "You can reward $rewardScore points"
            binding.tvOption.text = "Claim"
        } else if (loserIndex == 0) {
            binding.tvWinnerValue.text = "Player $winnerIndex"
            binding.tvLoserValue.text = "You"
            binding.tvCongratulations.text = "Keep trying"
            binding.tvReward.text = ""
            binding.tvOption.text = "Back"
        } else {
            binding.tvWinnerValue.text = "Player $winnerIndex"
            binding.tvLoserValue.text = "Player $loserIndex"
            binding.tvCongratulations.text = "Make persistent efforts"
            binding.tvReward.text = "You can reward $rewardScore points"
            binding.tvOption.text = "Claim"
        }

        binding.tvLevel.text = "level $newLevel"
        binding.progressView.progress = Utils.getLevelProgress(newLevel)
    }
}